package com.seven.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Method;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class BaseServlet extends HttpServlet {

	private Method method = null;
	private String text = null;

	@Override
	public void service(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String methodName = req.getParameter("method");
		if (methodName==null) {
			
			methodName = "selectAll";
		}
		
		
		System.out.println(req.getCharacterEncoding() + "编码");

		System.out.println(methodName + "方法名");
		if (methodName == null || methodName.trim().isEmpty()) {
			throw new RuntimeException("运行时异常");
		}

		try {
			method = this.getClass().getMethod(methodName,
					HttpServletRequest.class, HttpServletResponse.class);
		} catch (Exception e) {
			
			throw new RuntimeException("-------" + methodName
					+ "(HttpServletRequest,HttpServletResponse) ");
		}
		
		try {
			text = (String) method.invoke(this, req, resp);
		} catch (Exception e) {
			System.out.println("=====" + methodName
					+ "(HttpServletRequest,HttpServletResponse) ");
			e.printStackTrace();
			
		}
		if (text != null) {
			  if(text.endsWith("*")){
				  System.out.println("");
				  req.getRequestDispatcher(text.substring(0, text.length()-1)).forward(req, resp);
			  }else{
				  System.out.println("");
				  resp.sendRedirect(text);
			  }

			
		}

	}
	

}
